
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:自动注册协议类
 *
 * @author jerryliu
 * @since 2015年11月4日 上午11:24:06
 */
public class RegisterByAutoTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public RegisterByAutoResponse request(String tempName) throws AppException {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        RegisterByAutoResponse respObj = new RegisterByAutoResponse();
        ResponsePackage resp = new RegisterResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_REGISTER_AUTO);
        params.put("tempname", tempName);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_REGISTER_AUTO);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (AppException e) {
            throw e;
        }
    }

    class RegisterResponsePackage extends HttpBaseResponsePackage<RegisterByAutoResponse> {

        @Override
        protected void handleResponse(RegisterByAutoResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "RegisterByAutoTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_REGISTER_AUTO.equalsIgnoreCase(cmd)) {
                        int respCode = jsonResp.optInt("code");
                        if (respCode == 1) {
                            JSONObject jsonResult = jsonResp.optJSONObject("data");
                            if (jsonResult != null) {
                                response.setOpenId(jsonResult.optString("kopenid"));
                                response.setUserName(jsonResult.optString("username"));
                                response.setNickName(jsonResult.optString("nickname"));
                                response.setPassword(jsonResult.optString("password"));
                                response.setUnixtime(jsonResult.optString("unixtime"));
                                response.setToken(jsonResult.optString("verifytoken"));
                                response.setKugouToken(jsonResult.optString("kgtoken"));
                                response.setKugouUserId(jsonResult.optString("kg_userid"));
                                String kubiNum = jsonResult.optString("kubinum");
                                response.setUnreadmails(jsonResult.optInt("unreadmails"));
                                if (!TextUtils.isEmpty(kubiNum)) {
                                    response.setKubiNum(Float.valueOf(kubiNum));
                                }
                            }
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public static class RegisterByAutoResponse extends ResponseMessage {
        private static final long serialVersionUID = 8776873993003977566L;

        private ArrayList<String> recommendUsernames;

        private String userName = "";

        private String nickName = "";

        private String password = "";

        private String unixtime = "";

        private String token = "";

        private String kugouToken = "";

        private String kugouUserId = "";

        private float kubiNum;

        private int unreadmails;

        private String openId;

        public String getOpenId() {
            return openId;
        }

        public void setOpenId(String openId) {
            this.openId = openId;
        }

        public ArrayList<String> getRecommendUsernames() {
            return recommendUsernames;
        }

        public void setRecommendUsernames(ArrayList<String> recommendUsernames) {
            this.recommendUsernames = recommendUsernames;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getNickName() {
            return nickName;
        }

        public void setNickName(String nickName) {
            this.nickName = nickName;
        }

        public String getUnixtime() {
            return unixtime;
        }

        public void setUnixtime(String unixtime) {
            this.unixtime = unixtime;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getKugouToken() {
            return kugouToken;
        }

        public void setKugouToken(String kugouToken) {
            this.kugouToken = kugouToken;
        }

        public String getKugouUserId() {
            return kugouUserId;
        }

        public void setKugouUserId(String kugouUserId) {
            this.kugouUserId = kugouUserId;
        }

        public float getKubiNum() {
            return kubiNum;
        }

        public void setKubiNum(float kubiNum) {
            this.kubiNum = kubiNum;
        }

        public int getUnreadmails() {
            return unreadmails;
        }

        public void setUnreadmails(int unreadmails) {
            this.unreadmails = unreadmails;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
